package com.bluexmicro.example.route

import android.content.Context
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
import com.bluexmicro.example.page.main.MainViewModel
import com.bluexmicro.example.page.transaction.TransactionViewModel

@Composable
fun NavigationHost2(navHostController: NavHostController) {
    val context = LocalContext.current
    val mainViewModel = viewModel<MainViewModel>()
    val transactionViewModel = viewModel<TransactionViewModel>()
    LaunchedEffect(Unit) {
        mainViewModel.loadLocalTasks(context)
        mainViewModel.loadLocalFastMode(context)
        transactionViewModel.loadLocalTasks(context)
    }
    NavHost(navHostController, startDestination = "SingleManual") {
        singleManualGraph(navHostController, context, mainViewModel)
        singleAutomaticGraph(navHostController, context, transactionViewModel)
        loopGraph(navHostController)
    }
}

fun NavGraphBuilder.singleManualGraph(
    navHostController: NavHostController,
    context: Context,
    mainViewModel: MainViewModel
) {
    navigation(startDestination = "1", route = "SingleManual") {
        composable("1") {
//            MainPage(navHostController, mainViewModel)
            TestPage("first"){

            }
        }
        composable(SingleManual.FILE.route) {
//            PeripheralPage(navHostController) {
//                mainViewModel.selectedDevice.value = it
//                navHostController.popBackStack()
//            }
        }
        composable(SingleManual.Device.route) {
//            FilePicker(onClose = {
//                navHostController.popBackStack()
//            }, onSelected = { file, address ->
//                mainViewModel.selectFile(context, file, address)
//                navHostController.popBackStack()
//            })
        }
    }
}

fun NavGraphBuilder.singleAutomaticGraph(
    navHostController: NavHostController,
    context: Context,
    transactionViewModel: TransactionViewModel
) {
    navigation(startDestination = SingleAutomatic.Guide.route, route = SingleAutomatic.Guide.root) {
        composable(SingleAutomatic.Guide.route) {
//            TransactionPage(navHostController, transactionViewModel)
        }
        composable(SingleAutomatic.FILE.route) {
//            FilePicker(onClose = {
//                navHostController.popBackStack()
//            }, onSelected = { file, address ->
//                transactionViewModel.selectFile(context, file, address)
//                navHostController.popBackStack()
//            })
        }
        composable(SingleAutomatic.Device.route) {
//            PeripheralPage(navHostController) {
//                transactionViewModel.selectedDevice.value = it
//                navHostController.popBackStack()
//            }
        }
    }
}

fun NavGraphBuilder.loopGraph(navController: NavHostController) {
    navigation(startDestination = FlowLoop.Guide.route, route = FlowLoop.Guide.root) {
        composable(FlowLoop.Guide.route) {

        }
        composable(FlowLoop.FILE.route) {
        }

    }
}


@Composable
fun TestPage(content: String, onNext: () -> Unit) {
    Button(onClick = onNext) {
        Text(content)
    }
}

sealed class SingleManual(
    val route: String,
) {

    val root = "SingleManual"

    object Guide : SingleManual("SingleManual/Guide")
    object FILE : SingleManual("SingleManual/file")
    object Device : SingleManual("SingleManual/device")
}

sealed class SingleAutomatic(
    val route: String,
) {

    val root = "SingleAutomatic"

    object Guide : SingleManual("SingleAutomatic/Guide")
    object FILE : SingleManual("SingleAutomatic/file")
    object Device : SingleManual("SingleAutomatic/device")
}

sealed class FlowLoop(
    val route: String,
) {
    val root = "FlowLoop"

    object Guide : SingleManual("FlowLoop/Guide")
    object FILE : SingleManual("FlowLoop/file")
}

